package cn.stronglink.assetmanage.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.ArrayMap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.entity.TaskEntity;
import cn.stronglink.assetmanage.listener.MenuRecyclerListener;

/**
 * Author by chencongcong,Date on 2018/9/18
 */
public class MenuTitleAdapter extends RecyclerView.Adapter<MenuTitleAdapter.MenuHolder> {
    public ArrayMap<Integer, MenuHolder> maps = new ArrayMap<>();
    private Context mContext;
    private List<TaskEntity> menuDatas;
    private MenuRecyclerListener listener;
    private int selectPosition;

    public int getSelectPosition() {
        return selectPosition;
    }

    public void setSelectPosition(int selectPosition) {
        this.selectPosition = selectPosition;
        this.notifyDataSetChanged();
    }

    public MenuTitleAdapter(Context context) {
        mContext = context;
    }

    @Override
    public MenuHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = View.inflate(mContext, R.layout.item_menu_title, null);
        return new MenuHolder(inflate, listener);
    }

    @Override
    public void onBindViewHolder(MenuHolder holder, int position) {
        if (menuDatas != null) {
            holder.ivMenu.setImageResource(menuDatas.get(position).getTaskResuorces());
            holder.tvMenu.setText(menuDatas.get(position).getTaskText());
            if (position == selectPosition) {
                //默认选中第0个
                holder.ll_menu.setBackground(mContext.getDrawable(R.drawable.bg_menu_focus));
                holder.tvMenu.setTextColor(mContext.getResources().getColor(R.color.colorLoginInfo));
            } else {
                holder.ll_menu.setBackground(mContext.getDrawable(R.drawable.bg_edittext));
                holder.tvMenu.setTextColor(mContext.getResources().getColor(R.color.colorBlack));
            }
            maps.put(position, holder);
        }
    }

    @Override
    public int getItemCount() {
        if (menuDatas != null) {
            return menuDatas.size();
        }
        return 0;
    }

    public void setOnItemClickListener(MenuRecyclerListener listener) {
        this.listener = listener;
    }

    public void notifyDatasChanged(List<TaskEntity> menuDatas) {
        this.menuDatas = menuDatas;
        notifyDataSetChanged();
    }


    public class MenuHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @BindView(R.id.iv_menu)
        public ImageView ivMenu;
        @BindView(R.id.tv_menu)
        public TextView tvMenu;
        @BindView(R.id.ll_menu)
        public LinearLayout ll_menu;
        private MenuRecyclerListener listener;

        public MenuHolder(@NonNull View itemView, MenuRecyclerListener listener) {
            super(itemView);
            this.listener = listener;
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (listener != null) {
                listener.onMenuItemClicked(v, getAdapterPosition());
            }
        }
    }

}
